package com.yaocw.baseproject.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.yaocw.baseproject.R;

public class NavigationBar extends FrameLayout {
    private TextView tvTitle;
    private ImageView ivLeftImage;
    private ImageView ivRightImage;

    public NavigationBar(@NonNull Context context) {
        super(context);
        initView(context);
    }

    public NavigationBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public NavigationBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        View view = inflate(context, R.layout.navgation_bar, this);
        tvTitle = view.findViewById(R.id.tv_nav_title);
        ivLeftImage = view.findViewById(R.id.iv_nav_back);
        ivRightImage = view.findViewById(R.id.iv_nav_right);

        ivRightImage.setVisibility(View.INVISIBLE);
    }

    public void setTitle(String title) {
        tvTitle.setText(title);
    }

    public void hideLeftButton() {
        ivLeftImage.setVisibility(View.INVISIBLE);
    }

    public void setLeftButton(Integer image, OnClickListener listener) {
        ivLeftImage.setVisibility(View.VISIBLE);

        if (image != null) {
            ivLeftImage.setImageResource(image);
        }

        if (listener != null) {
            ivLeftImage.setOnClickListener(listener);
        }
    }

    public void setRightButton(Integer image, OnClickListener listener) {
        if (image != null) {
            ivRightImage.setImageResource(image);
            ivRightImage.setVisibility(View.VISIBLE);

            if (listener != null) {
                ivRightImage.setOnClickListener(listener);
            }
        } else {
            ivRightImage.setVisibility(View.INVISIBLE);
        }
    }

}
